﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_GiaiDoanController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        //
        // GET: /GPS_HTCapGPLX/
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        #region Index
        public ActionResult Index(ViewModelSGIAIDOAN SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SGIAIDOAN>(o => (SearchModel.gGDOAN_MA == null || o.GDOAN_MA.StartsWith(SearchModel.gGDOAN_MA))
                                        && (SearchModel.gGDOAN_CSDT_MA == null || o.GDOAN_CSDT_MA.StartsWith(SearchModel.gGDOAN_CSDT_MA)));
            ViewBag.TotalRow = entities.Count();
            var model = new List<SGIAIDOAN>();
            Int16 icount = 1;
            foreach (var currentEntity in entities)
            {
                currentEntity.STT = icount;
                icount++;
                model.Add(Mapper.Map<SGIAIDOAN>(currentEntity));
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSGIAIDOAN viewModel)
        {
            viewModel.listgiaothong = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "DT" && x.DVGTVT_MAQL == "40").ToList();
            ViewBag.lstDVDaoTao = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "DT" && x.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.checkuser = this.getCurrentUser().MA_TINHTP;
        }

        public ActionResult ExportToExcel()
        {
            GridView gv = new GridView();
            gv.DataSource = _db.GetAll<SGIAIDOAN>().ToList();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=Marklist.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            return RedirectToAction("StudentDetails");
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSGIAIDOAN());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSGIAIDOAN model)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SGIAIDOAN _model = new SGIAIDOAN();
                GPS.COMMON.Helpers.CopyObject<SGIAIDOAN>(model, ref _model);
                //_model.TTXL_MA = GetValue_Indentity();
                _db.Insert<SGIAIDOAN>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            ViewBag.lstDVDaoTao = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "DT" && x.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string id, string id1)
        {
            CrudModelSGIAIDOAN _model = new CrudModelSGIAIDOAN();
            SGIAIDOAN model = _db.GetOne<SGIAIDOAN>(o => o.GDOAN_MA == id && o.GDOAN_CSDT_MA == id1);
            GPS.COMMON.Helpers.CopyObject<CrudModelSGIAIDOAN>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSGIAIDOAN model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SGIAIDOAN>(o => o.GDOAN_MA == model.GDOAN_MA);
                GPS.COMMON.Helpers.CopyObject<SGIAIDOAN>(model, ref _model);
                _db.Update<SGIAIDOAN>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelSGIAIDOAN view)
        {
            ViewBag.lstDVDaoTao = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "DT" && x.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        }
        #endregion

        #region Detele
        public ActionResult Delete(string id, string id1)
        {
            CrudModelSGIAIDOAN _model = new CrudModelSGIAIDOAN();
            SGIAIDOAN model = _db.GetOne<SGIAIDOAN>(o => o.GDOAN_MA == id && o.GDOAN_CSDT_MA == id1);
            _db.DeleteItem<SGIAIDOAN>(model);
            return RedirectToAction("Index");
        }
        #endregion

        #region Detail
        public ActionResult Detail(string id, string id1)
        {
            CrudModelSGIAIDOAN _model = new CrudModelSGIAIDOAN();
            SGIAIDOAN model = _db.GetOne<SGIAIDOAN>(o => o.GDOAN_MA == id && o.GDOAN_CSDT_MA == id1);
            GPS.COMMON.Helpers.CopyObject<CrudModelSGIAIDOAN>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelSGIAIDOAN view)
        {
            var tencosodaotao = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == "40" && x.DVGTVT_MA == view.GDOAN_CSDT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            view.GDOAN_CSDT_MA = tencosodaotao.ToString();
        }
        #endregion
    }
}